#!/bin/bash
########################################################################
# script name:  sync_keys.sh
# Used for:     Syncing ssh keys everywhere
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/04/06    0.1     MDUMONT         initial
# 2011/04/07    1.o     MDUMONT         official release
########################################################################
usage()
{
cat << STOP

usage: $0 [options] [ARG]

This script is used for ....

OPTIONS:
   -h               Comma seperated list of hosts, or ALL
   -u               Comma seperated list of users
   -c               Clear existing authorized_keys before editing (cleanup)

Ex.:
$0 -h saprmt02
$0 -h saprmt02,saprmt01 -c
$0 -h ALL

STOP
}
#######################################
# Get parameters
#######################################
while getopts h:u:c OPTION
do
        case $OPTION in
        h)
                HOSTS=$(echo $OPTARG | tr '[A-Z]' '[a-z]' | sed 's/,/ /g')
                ;;
		  u)
                USERLIST=$(echo $OPTARG | tr '[A-Z]' '[a-z]' | sed 's/,/ /g')
                ;;
        c)
                CLEAR=on
                ;;
        \?)
                usage
                exit 1
                ;;
        *)
                usage
                exit 1
                ;;
        esac
done
if [ $OPTIND = 1 ]; then usage; exit 1; fi
if [ -z "$HOSTS" ]; then echo "Specify a host..."; usage; exit 1; fi
if [ -z "$USERLIST" ]; then USERLIST=$USER; fi
if [ ! -z "$CLEAR" ]
then
  read -p "Are you sure you want to CLEAR the remote authentification file (default no)? [y/n] " -e a1
  if [ "$a1" != "y" ]; then echo "Exiting."; exit 1; fi
fi
#######################################
# Functions
#######################################
SSH_OPTS=" -x -o ConnectTimeout=15 -o BatchMode=yes -o StrictHostKeyChecking=no -o LogLevel=quiet"
BAENV=$(dirname `which $0`)/loadenv.sh
if [ -f $BAENV ]; then . $BAENV; else echo "No env library ($BAENV)"; exit 1; fi
function check_identity {
	ID_FILE="`cat /etc/passwd | awk -v h=$1 -F: '{if ($1==h) print $6}'`/.ssh/id_rsa.pub"
	GET_ID="su $1 -c cat ${ID_FILE}"
	if [ -z "`eval $GET_ID`" ]; then
		echo "$0: ERROR: No identities found" >&2
		exit 1
	fi
	echo -e "Verifying Public key $ID_FILE ... \c"
	su $1 -c ssh-keygen -B -f $ID_FILE 2>&1 > /dev/null
	RC=$?
	if [ $RC = 0 ]; then echo ok; else echo not ok; exit 1; fi
}
#######################################
# MAIN
#######################################
NEWHOSTLIST=$(checkhost $HOSTS | grep -v Verifying)
for TGUSER in $USERLIST
do
	check_identity $TGUSER
	for HOSTNAME in $NEWHOSTLIST
	do
		echo -e "Syncing public key from $HOST to $TGUSER on $HOSTNAME ... \c"
		if [ "$CLEAR" = "on" ]
		then
			{ eval "$GET_ID" ; } | su saunix ssh $SSH_OPTS saunix@$HOSTNAME "sudo -u $TGUSER sh -c 'test -d ~$TGUSER/.ssh || sudo -u $TGUSER mkdir -m 700 ~$TGUSER/.ssh ; cat > ~$TGUSER/.ssh/authorized_keys; chmod 600 ~$TGUSER/.ssh/authorized_keys'"
			echo -e "ok"
		else
			{ eval "$GET_ID" ; } | sudo -u saunix ssh $SSH_OPTS saunix@$HOSTNAME "sudo -u $TGUSER sh -c 'test -d ~$TGUSER/.ssh || mkdir -m 700 ~$TGUSER/.ssh ; cat >> ~$TGUSER/.ssh/authorized_keys; chmod 600 ~$TGUSER/.ssh/authorized_keys; chown -R $TGUSER ~$TGUSER/.ssh'"
			echo -e "ok"
		fi
	done
done
#######################################
exit 0
